Skip to content

fix(content-gate): cascade taxonomy content rules to child terms (NPPD-1670)#283

Open
adekbadek wants to merge 1 commit into
releasefrom
nppd-1670-content-rules-handle-child-terms-for-hierarchical-taxonomies
Open

fix(content-gate): cascade taxonomy content rules to child terms (NPPD-1670)#283
adekbadek wants to merge 1 commit into
releasefrom
nppd-1670-content-rules-handle-child-terms-for-hierarchical-taxonomies

Conversation

@adekbadek

@adekbadek adekbadek commented Jun 11, 2026

Copy link
Copy Markdown
Member

All Submissions:

Changes proposed in this Pull Request:

Fixes NPPD-1670. Content gates with taxonomy-term content rules now cascade to a targeted term's child terms for hierarchical taxonomies (categories), matching WooCommerce Memberships.

Previously Content_Restriction_Control::get_post_gates() matched a post's term IDs against a rule's term IDs with a flat array_intersect, so a rule targeting a parent category did not gate posts assigned only to a child category. Restricted content leaked: a publisher reported no paywall appearing on child-category posts.

The fix adds a private expand_hierarchical_terms() helper that expands a hierarchical-taxonomy rule's target terms to include all descendants (via get_term_children()) before the intersect. Key properties:

  • Evaluation-time expansion, so child terms added after a rule is saved are covered automatically (matches Woo's dynamic behavior).
  • Symmetric for inclusion and exclusion rules.
  • No-op for non-hierarchical taxonomies (tags), and no public-contract change (private method only) — the cross-repo newspack-manager consumer is unaffected.

How to test the changes in this Pull Request:

  1. Create a category tree (parent → child → grandchild) and a post assigned only to the child or grandchild category (not the parent).
  2. Create a content gate with a content rule targeting the parent category, with registration or custom access active.
  3. Visit the child/grandchild post as a logged-out reader → the gate (paywall/regwall) now appears. Before this change it did not.
  4. Flip the rule to an exclusion rule on the parent → the child/grandchild post is no longer gated, while posts outside the subtree still are.
  5. Or run the unit tests: n test-php --filter test_content_rules (covers inclusion cascade, exclusion cascade, the one-directional guarantee, and non-hierarchical passthrough).

Rollout note

Because expansion is evaluation-time, on deploy every existing gate targeting a parent category recomputes its gated-post set with no migration step — this is a behavior-changing deploy, not a transparent fix. A per-site audit of the currently-live Access-Control sites (which gates expand and by how much) is tracked on NPPD-1670.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally? (Test_Content_Gates: 56 tests / 167 assertions green; PHPCS clean.)

Copilot AI review requested due to automatic review settings June 11, 2026 09:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a content-gate evaluation gap where taxonomy-term rules didn’t cascade from a parent term to its descendants for hierarchical taxonomies (e.g., categories), which could allow restricted content to appear ungated on child-category posts. The change aligns Newspack’s behavior with WooCommerce Memberships by expanding hierarchical rule targets at evaluation time.

Changes:

  • Expand hierarchical taxonomy rule term IDs to include descendant terms before evaluating post matches.
  • Add unit tests covering inclusion cascade, exclusion cascade, one-directional behavior, and non-hierarchical taxonomy behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
plugins/newspack-plugin/includes/content-gate/class-content-restriction-control.php Expands hierarchical taxonomy rule targets (via get_term_children()) before intersecting with a post’s assigned term IDs.
plugins/newspack-plugin/tests/unit-tests/content-gate/content-gates.php Adds unit tests validating hierarchical descendant cascading and non-hierarchical passthrough behavior.

@adekbadek adekbadek marked this pull request as ready for review June 11, 2026 10:11
@adekbadek adekbadek requested a review from a team as a code owner June 11, 2026 10:11
…D-1670)

Content gates with taxonomy-term content rules now include a targeted
term's descendants for hierarchical taxonomies, matching the cascade
behavior of WooCommerce Memberships. Previously a rule targeting a parent
category did not gate posts assigned only to a child category, so
restricted content could leak.

Expansion happens at evaluation time via get_term_children(), so child
terms added after a rule is saved are covered automatically. Symmetric
for inclusion and exclusion rules; non-hierarchical taxonomies (tags) are
unaffected.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@adekbadek adekbadek force-pushed the nppd-1670-content-rules-handle-child-terms-for-hierarchical-taxonomies branch from 8da8d6b to fbdf5ea Compare June 11, 2026 13:00
@adekbadek adekbadek changed the base branch from main to release June 11, 2026 13:00
@adekbadek adekbadek marked this pull request as draft June 11, 2026 13:01
@adekbadek adekbadek marked this pull request as ready for review June 11, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants